package com.jqsoft.launcher.activity.setting;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.activity.SettingIPActivity;
import com.jqsoft.launcher.common.BphsConstants;
import com.jqsoft.launcher.R;
import com.jqsoft.launcher.util.DownLoadUtil;

import java.io.File;

public class SettingActivity extends JQActivity implements
		View.OnClickListener {
	private final String tag = "SettingActivity";
	private RelativeLayout setting_ip_rl, xiazai_cj_rl, banbenupdate_rl,
			xiazai_xindian_rl;
	private ProgressDialog pd;
	private static final int DOWNLOAD_SUCCESS = 26;
	private static final int DOWNLOAD_ERROR = 27;

	@Override
	protected int getContentLayout() {

		return R.layout.setting_list_layout;
	}

	@Override
	protected void initGui() {
		setting_ip_rl = (RelativeLayout) findViewById(R.id.setting_ip_rl);
//		xiazai_cj_rl = (RelativeLayout) findViewById(R.id.xiazai_cj_rl);
//		banbenupdate_rl = (RelativeLayout) findViewById(R.id.banbenupdate_rl);
//		xiazai_xindian_rl = (RelativeLayout) findViewById(R.id.xiazai_xindian_rl);

	}

	@Override
	protected void initAction() {
		setGoBack();
//		xiazai_cj_rl.setOnClickListener(this);
 	setting_ip_rl.setOnClickListener(this);
//		banbenupdate_rl.setOnClickListener(this);
//		xiazai_xindian_rl.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		setTitle("设置功能");

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_ip_rl:
			toNoFinish(SettingIPActivity.class);
			return;
//		case R.id.title_bar_left_button:
//			toHome();
//			return;
//		case R.id.xiazai_cj_rl:
//			if (!BphsConstants.isNetAvailable) {
//				MessageTools.showDialogOk(ctx, "此功能暂不支持离线操作，请联网操作！");
//				return;
//			}
//			showUpdateDialog("LanYa.apk");
//			return;
//		case R.id.banbenupdate_rl:
//			toNoFinish(BanbenUpdateActivity.class);
//			return;
//		case R.id.xiazai_xindian_rl:
//			if (!BphsConstants.isNetAvailable) {
//				MessageTools.showDialogOk(ctx, "此功能暂不支持离线操作，请联网操作！");
//				return;
//			}
//			showUpdateDialog("XinDian.apk");
//			return;
		}

	}

	/**
	 * 安装一个apk文件 只要知道安装的路径
	 *
	 * @param file
	 */
	protected void installApk(File file) {
		/*
		 * <intent-filter> <action android:name="android.intent.action.VIEW" />
		 * <category android:name="android.intent.category.DEFAULT" /> <data
		 * android:scheme="content" /> <data android:scheme="file" /> <data
		 * android:mimeType="application/vnd.android.package-archive" />
		 * </intent-filter>
		 */
		// 1.显示意图
		// 2.隐式意图

		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		// intent.addCategory("android.intent.category.DEFAULT");
		/*
		 * intent.setType("application/vnd.android.package-archive");
		 * intent.setData(Uri.fromFile(file));
		 */
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		startActivity(intent);
		// 泡茶 泡咖啡 泡妞 泡红茶 泡绿茶 打3瓶海天酱油
		// addCategory 额外的配置信息.
	}

	private Handler myhandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {

			case DOWNLOAD_SUCCESS:
				// Logger.i(TAG,"文件下载成功");
				Toast.makeText(getApplicationContext(), "下载成功，即将安装", 1).show();
				File file = (File) msg.obj;
				installApk(file);
				break;
			case DOWNLOAD_ERROR:
				Toast.makeText(getApplicationContext(), "下载数据异常", 1).show();
				// loadDictionary();
				break;
			}
		};
	};

	/**
	 * 显示升级提示的对话框
	 */
	protected void showUpdateDialog(final String filemin) {
		//创建了对话框的构造器
		Builder builder = new Builder(this);
		builder.setIcon(getResources().getDrawable(R.drawable.logo_bg));
		builder.setTitle("温馨提示");
		builder.setMessage("此控件是为了你更好的使用本应用程序，建议下载安装，和及时更新。");
		pd = new ProgressDialog(SettingActivity.this);
		pd.setMessage("正在下载");
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 指定显示进度条
		builder.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
//				Logger.i(TAG, "升级,下载" + info.getApkurl());
				if (Environment.MEDIA_MOUNTED.equals(Environment
						.getExternalStorageState())) {
					pd.show();
					new Thread() {
						public void run() {
							// 生成环境
							String result = BphsConstants.HTTP_PROTOCOL
									+ BphsConstants.HTTP_SERVER_IP + ":"
									+ BphsConstants.HTTP_SERVER_PORT + "/"
									;
							String path = result+filemin;
//							String path = "http://192.168.44.163:9099/LanYa.apk";
//							String path = "http://192.168.44.16filename3:9099/GongWei.apk";
							String filename="";
							if("LanYa.apk".equals(filemin)){
								 filename = "蓝牙插件.apk";
							}else if ("XinDian.apk".equals(filemin)) {
								 filename = "心电插件.apk";
							}
							
							// 创建一个文件夹对象，赋值为外部存储器的目录
				            File sdcardDir =Environment.getExternalStorageDirectory();
				          //得到一个路径，内容是sdcard的文件夹路径和名字
				            File path1 = new File(sdcardDir.getPath()+"/合肥晶奇资料");
				            if (!path1.exists()) {
				            	   path1.mkdirs();
				            }
							File file = new File(path1,filename);
							file = DownLoadUtil.getFile( ctx,path, file.getAbsolutePath(), pd);
							if(file!=null){
								Message msg = Message.obtain();
								msg.what = DOWNLOAD_SUCCESS;
								msg.obj = file;
								myhandler.sendMessage(msg);
							}else{
								Message msg = Message.obtain();
								msg.what = DOWNLOAD_ERROR;
								myhandler.sendMessage(msg);
							}
							pd.dismiss();
						};
					}.start();
				}else {
					Toast.makeText(getApplicationContext(), "sd卡不可用", 1).show();

				}
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
//				loadDictionary();  

			}

		});
		AlertDialog dialog =builder.create();
		dialog.setCancelable(false);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
//		builder.
//		builder.create().show();
	}
}
